include constants.doh

/// Purpose: Generate initial SY migrant sample results, including regional averages


////////////////////////////////////////////
///////// Read in SY Migrants Data /////////
////////////////////////////////////////////

// See the README for instructions on how to access this data
import delimited "input/pipeline/sy_regressions_input.csv", clear

replace curr_nuts3 = "_dummy" if curr_nuts3 == "`baseline_region'"
encode curr_nuts3, gen(nuts3_encoded)
fvset base last nuts3_encoded

// Make the total friend/group/cont produced controls
gen ln_other_frnd = log(total_30d_other_country_frnds + 1)
gen ln_other_grps = log(other_grps + 1)
gen ln_cont_prod = log(total_content_produced_yr + 1)

winsor2 n_frnd_nat_lcl, cuts(0 99) replace
winsor2 n_frnd_nat_50_lcl, cuts(0 99) replace
winsor2 pct_frnd_nat_lcl, cuts(0 99) replace
winsor2 n_lcl_50n_grps, cuts(0 99) replace
winsor2 shr_grps_lcl_50n, cuts(0 99) replace
winsor2 de_grps_avg_shr_nativ, cuts(0 99) replace
winsor2 pct_produ_de, cuts(0 99) replace
winsor2 pct_consu_de, cuts(0 99) replace
winsor2 n_frnd_sy_lcl, cuts(0 99) replace
winsor2 n_frnd_eu_lcl, cuts(0 99) replace
winsor2 n_frnd_t5_lcl, cuts(0 99) replace
winsor2 n_frnd_recent_t5_lcl, cuts(0 99) replace


////////////////////////////////////////////////////////////
/// Use the regional FEs to try to predict demographics ///
////////////////////////////////////////////////////////////

eststo clear

eststo panel1_age: reghdfe age, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel2_female: reghdfe is_female, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel3_quarters_de: reghdfe quarters_since_de, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel4_total_frnds: reghdfe total_30d_frnds, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel5_n_frnd_nat_lcl: reghdfe n_frnd_nat_lcl, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel6_indiv_time_cntrl: reghdfe indiv_time_cntrl, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel7_indiv_l90_cntrl: reghdfe indiv_l90_cntrl, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel8_indiv_l1080_cntrl: reghdfe indiv_l1080_cntrl, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm

custom_xml_tab "output" "nuts3_fe_rsq"

eststo clear


//////////////////////////////////////////////////////
//// Individual level correlations & binscatters ////
/////////////////////////////////////////////////////

///// A All the outcomes --- uncontrolled //////

/// A1 Correlation table
eststo clear

estpost correlate n_frnd_nat_lcl n_frnd_nat n_frnd_nat_50 pct_frnd_nat n_frnd_sy_lcl n_frnd_t5_lcl n_frnd_recent_t5_lcl pct_produ_de pct_consu_de act_de produ_any_de consu_any_de n_lcl_50n_grps shr_grps_lcl_50n de_grps_avg_shr_nativ, matrix
eststo corrtr

esttab using output/sy_user_corr_big.csv, nomtitles unstack nostar nonumbers nodepvars not noobs compress b(%9.2f) replace

/// A2 Frnds vs Content Consumed
binscatter pct_produ_de n_frnd_nat_lcl, ytitle(% Content Produced in DE) xtitle(N Local Native Friends) reportreg n(80) line(qfit) mcolor("black") lcolor("blue") `binscat_defaults'
graph export "output/n_frnd_nat_lcl_VS_pct_produ_de.pdf"

/// A3 Native Frnds vs Grps
binscatter n_lcl_50n_grps n_frnd_nat_lcl, ytitle(N Local Native Groups) xtitle(N Local Native Friends) reportreg n(80) line(qfit) mcolor("black") lcolor("blue") `binscat_defaults'
graph export "output/n_frnd_nat_lcl_VS_n_lcl_50n_grps.pdf"


///// B Main outcomes --- controlling for usage /////

local outcomes = "n_frnd_nat_lcl n_frnd_nat n_frnd_nat_50 pct_frnd_nat n_frnd_sy_lcl n_frnd_t5_lcl n_frnd_recent_t5_lcl pct_produ_de pct_consu_de act_de produ_any_de consu_any_de n_lcl_50n_grps shr_grps_lcl_50n de_grps_avg_shr_nativ"

foreach curr_outcome in `outcomes' {

    reghdfe `curr_outcome' `nuts3_linear_cntrls', absorb(i.nuts3_encoded i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5) ///
        resid(R_`curr_outcome')
    egen M_`curr_outcome' = mean(`curr_outcome')
    replace R_`curr_outcome' = R_`curr_outcome' + M_`curr_outcome'

}

/// B1 Correlation table
eststo clear

estpost correlate R_n_frnd_nat_lcl R_n_frnd_nat R_n_frnd_nat_50 R_pct_frnd_nat R_n_frnd_sy_lcl R_n_frnd_t5_lcl R_n_frnd_recent_t5_lcl R_pct_produ_de R_pct_consu_de R_act_de R_produ_any_de R_consu_any_de R_n_lcl_50n_grps R_shr_grps_lcl_50n R_de_grps_avg_shr_nativ, matrix
eststo corrtr

esttab using output/sy_user_corr_big_RESID.csv, nomtitles unstack nostar nonumbers nodepvars not noobs compress b(%9.2f) replace

/// B2 Frnds vs Content Consumed
binscatter R_pct_produ_de R_n_frnd_nat_lcl, ytitle(% Content Produced in DE) xtitle(N Local Native Friends) reportreg n(50) line(qfit) mcolor("black") lcolor("blue") `binscat_defaults'

graph export "output/n_frnd_nat_lcl_VS_pct_produ_de_RESID.pdf"

/// B3 Native Frnds vs Grps
binscatter R_n_lcl_50n_grps R_n_frnd_nat_lcl, ytitle(N Local Native Groups) xtitle(N Local Native Friends) reportreg n(50) line(qfit) mcolor("black") lcolor("blue") `binscat_defaults'

graph export "output/n_frnd_nat_lcl_VS_n_lcl_50n_grps_RESID.pdf"

drop R_*
drop M_*


////////////////////////////////////////
//// Individual level determinants /////
////////////////////////////////////////

eststo clear

eststo panel1: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3) tol(1e-5)
estadd ysumm
eststo panel2: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel3: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel4: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female  indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm

eststo panel5: reghdfe produ_any_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3) tol(1e-5)
estadd ysumm
eststo panel6: reghdfe produ_any_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel7: reghdfe produ_any_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel8: reghdfe produ_any_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female  indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm

eststo panel9: reghdfe n_lcl_50n_grps age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3) tol(1e-5)
estadd ysumm
eststo panel10: reghdfe n_lcl_50n_grps age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel11: reghdfe n_lcl_50n_grps age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel12: reghdfe n_lcl_50n_grps age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female  indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm

custom_xml_tab "output" "deters_baseline"

eststo clear

eststo clear

eststo panel1: reghdfe n_frnd_nat age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel2: reghdfe n_frnd_nat_50 age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel3: reghdfe pct_frnd_nat age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel4: reghdfe pct_produ_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel5: reghdfe pct_consu_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel6: reghdfe act_de age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel7: reghdfe shr_grps_lcl_50n age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel8: reghdfe de_grps_avg_shr_nativ age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.quarters_since_de i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm

custom_xml_tab "output" "deters_other"

eststo clear


//////////////////////////////////////////////////
/// Generate Regional SY Migrant coefficients ////
//////////////////////////////////////////////////

///////// First just the averages, Ns, and variances outside of a regression framework (main outcomes and other outcomes) //////////

preserve

    collapse (mean) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de n_frnd_sy_lcl n_frnd_eu_lcl n_frnd_t5_lcl n_frnd_recent_t5_lcl (count) n=n_frnd_nat_lcl, by(nuts3_encoded)

    export delimited "output/sy_migrant_nuts3_avgs_and_Ns.csv"

restore

preserve

    collapse (sd) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de n_frnd_sy_lcl n_frnd_eu_lcl n_frnd_t5_lcl n_frnd_recent_t5_lcl, by(nuts3_encoded)

    export delimited "output/sy_migrant_nuts3_sds.csv"

restore


//////// Then the FEs ////////////

/// Main outcomes ///

eststo clear

eststo panel1: reghdfe n_frnd_nat_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm
eststo panel2: reghdfe n_lcl_50n_grps `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm
eststo panel3: reghdfe produ_any_de `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm

esttab panel* using "output/nuts3_FEs_sy_migrant_primary.csv", se label replace

eststo clear


/// Other outcomes ///

eststo clear

eststo panel1: reghdfe n_frnd_sy_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm
eststo panel2: reghdfe n_frnd_eu_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm
eststo panel3: reghdfe n_frnd_t5_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm
eststo panel4: reghdfe n_frnd_recent_t5_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm

esttab panel* using "output/nuts3_FEs_sy_migrant_other_fr.csv", se label replace

eststo clear


//////// Main outcomes by gender //////////

local grps = "0 1"

foreach curr_grp in `grps' {

    //// First the uncontrolled averages

    preserve

        keep if is_female == `curr_grp'
        collapse (mean) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de (count) n=n_frnd_nat_lcl, by(nuts3_encoded)
        export_delimited "output/sy_migrant_nuts3_avgs_and_Ns_isfemale_`curr_grp'.csv"

    restore

    preserve

        keep if is_female == `curr_grp'
        collapse (sd) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de, by(nuts3_encoded)
        export_delimited "output/sy_migrant_nuts3_sds_isfemale_`curr_grp'.csv"

    restore


    //// Then the FEs

    eststo clear

    eststo panel1: reghdfe n_frnd_nat_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
        if is_female == `curr_grp'
    estadd ysumm
    eststo panel2: reghdfe n_lcl_50n_grps `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
        if is_female == `curr_grp'
    estadd ysumm
    eststo panel3: reghdfe produ_any_de `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
        if is_female == `curr_grp'
    estadd ysumm

    esttab panel* using "output/nuts3_FEs_sy_migrant_primary_isfemale_`curr_grp'.csv", se label replace

    eststo clear
}

//////// Main outcomes by age group //////////

local grps = "18_24 25_34 35_44 45_54 55"

foreach curr_grp in `grps' {

    //// First the uncontrolled averages

    preserve

        keep if age_brkt_`curr_grp' == 1
        collapse (mean) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de (count) n=n_frnd_nat_lcl, by(nuts3_encoded)
        export delimited "output/sy_migrant_nuts3_avgs_and_Ns_age_`curr_grp'.csv"

    restore

    preserve

        keep if age_brkt_`curr_grp' == 1
        collapse (sd) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de, by(nuts3_encoded)
        export delimited "output/sy_migrant_nuts3_sds_age_`curr_grp'.csv"

    restore


    //// Then the FEs

    eststo clear

    eststo panel1: reghdfe n_frnd_nat_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
        if age_brkt_`curr_grp' == 1
    estadd ysumm
    eststo panel2: reghdfe n_lcl_50n_grps `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
        if age_brkt_`curr_grp' == 1
    estadd ysumm
    eststo panel3: reghdfe produ_any_de `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
        if age_brkt_`curr_grp' == 1
    estadd ysumm

    esttab panel* using "output/nuts3_FEs_sy_migrant_primary_age_`curr_grp'.csv", se label replace

    eststo clear
}


/////////////////////////////
///// Split sample test ////
////////////////////////////

set seed 17 // set a seed for reproducibility

// Now split the sample giving us equal buckets by NUTS3
splitsample, generate(svar) balance(curr_nuts3)


/// Raw Averages ///

preserve

    collapse (mean) n_frnd_nat_lcl n_lcl_50n_grps produ_any_de n_frnd_sy_lcl n_frnd_eu_lcl n_frnd_t5_lcl n_frnd_recent_t5_lcl (count) n=n_frnd_nat_lcl, by(svar nuts3_encoded)

    export delimited "output/sy_migrant_nuts3_avgs_and_Ns_split_sample.csv"

restore

/// Coeffs - Main outcomes ///

eststo clear

eststo panel1: reghdfe n_frnd_nat_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 1
estadd ysumm
eststo panel2: reghdfe n_frnd_nat_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 2
estadd ysumm

eststo panel3: reghdfe n_lcl_50n_grps `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 1
estadd ysumm
eststo panel4: reghdfe n_lcl_50n_grps `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 2
estadd ysumm

eststo panel5: reghdfe produ_any_de `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 1
estadd ysumm
eststo panel6: reghdfe produ_any_de `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 2
estadd ysumm


esttab panel* using "output/nuts3_FEs_sy_migrant_primary_split_sample.csv", se label replace

eststo clear


/// Coeffs - Other outcomes ///

eststo clear

eststo panel1: reghdfe n_frnd_sy_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 1
estadd ysumm
eststo panel2: reghdfe n_frnd_sy_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 2
estadd ysumm

eststo panel3: reghdfe n_frnd_eu_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 1
estadd ysumm
eststo panel4: reghdfe n_frnd_eu_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 2
estadd ysumm

eststo panel5: reghdfe n_frnd_t5_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 1
estadd ysumm
eststo panel6: reghdfe n_frnd_t5_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 2
estadd ysumm

eststo panel7: reghdfe n_frnd_recent_t5_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 1
estadd ysumm
eststo panel8: reghdfe n_frnd_recent_t5_lcl `nuts3_linear_cntrls' i.nuts3_encoded, vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5), ///
    if svar == 2
estadd ysumm

esttab panel* using "output/nuts3_FEs_sy_migrant_other_fr_split_sample.csv", se label replace

eststo clear



/////////////////////////////////////////////////
///////// SY Migrants - Temporal Friending //////
/////////////////////////////////////////////////

// See the README for instructions on how to access this data
import delimited "input/pipeline/sy_regressions_temporal_input.csv", clear

replace curr_nuts3 = "_dummy" if curr_nuts3 == "`baseline_region'"
encode curr_nuts3, gen(nuts3_encoded)
fvset base last nuts3_encoded

// Make the total friend/group/cont produced controls
gen ln_other_frnd = log(total_30d_other_country_frnds + 1)
gen ln_other_grps = log(other_grps + 1)
gen ln_cont_prod = log(total_content_produced_yr + 1)

egen p99_overall = pctile(n_frnd_nat_lcl_qrtr), p(99)

replace n_frnd_nat_lcl_qrtr = p99_overall if n_frnd_nat_lcl_qrtr != . & n_frnd_nat_lcl_qrtr > p99_overall
replace n_frnd_nat_lcl_15_16_qrtr = p99_overall if n_frnd_nat_lcl_15_16_qrtr != . & n_frnd_nat_lcl_15_16_qrtr > p99_overall
replace n_frnd_nat_lcl_17_18_qrtr = p99_overall if n_frnd_nat_lcl_17_18_qrtr != . & n_frnd_nat_lcl_17_18_qrtr > p99_overall
replace n_frnd_nat_lcl_19_20_qrtr = p99_overall if n_frnd_nat_lcl_19_20_qrtr != . & n_frnd_nat_lcl_19_20_qrtr > p99_overall


//////// First just the averages, Ns, and variances outside of a regression framework (main outcomes and other outcomes) //////////

preserve
    collapse (mean) n_frnd_nat_lcl_qrtr (count) n=n_frnd_nat_lcl_qrtr [aweight=n_qrtr], by(nuts3_encoded)
    export delimited "output/sy_migrant_nuts3_avgs_and_Ns_quarterly_all.csv"
restore
preserve
    collapse (sd) n_frnd_nat_lcl_qrtr [aweight=n_qrtr], by(nuts3_encoded)
    export delimited "output/sy_migrant_nuts3_sds_quarterly_all.csv"
restore


preserve
    collapse (mean) n_frnd_nat_lcl_15_16_qrtr (count) n=n_frnd_nat_lcl_qrtr [aweight=n_qrtr_15_16], by(nuts3_encoded)
    export delimited "output/sy_migrant_nuts3_avgs_and_Ns_quarterly_15_16.csv"
restore
preserve
    collapse (sd) n_frnd_nat_lcl_15_16_qrtr [aweight=n_qrtr_15_16], by(nuts3_encoded)
    export delimited "output/sy_migrant_nuts3_sds_quarterly_15_16.csv"
restore


preserve
    collapse (mean) n_frnd_nat_lcl_17_18_qrtr (count) n=n_frnd_nat_lcl_qrtr [aweight=n_qrtr_17_18], by(nuts3_encoded)
    export delimited "output/sy_migrant_nuts3_avgs_and_Ns_quarterly_17_18.csv"
restore
preserve
    collapse (sd) n_frnd_nat_lcl_17_18_qrtr [aweight=n_qrtr_17_18], by(nuts3_encoded)
    export delimited "output/sy_migrant_nuts3_sds_quarterly_17_18.csv"
restore


preserve
    collapse (mean) n_frnd_nat_lcl_19_20_qrtr (count) n=n_frnd_nat_lcl_qrtr [aweight=n_qrtr_19_20], by(nuts3_encoded)
    export delimited "output/sy_migrant_nuts3_avgs_and_Ns_quarterly_19_20.csv"
restore
preserve
    collapse (sd) n_frnd_nat_lcl_19_20_qrtr [aweight=n_qrtr_19_20], by(nuts3_encoded)
    export delimited "output/sy_migrant_nuts3_sds_quarterly_19_20.csv"
restore


//////// Then the FEs ////////////

/// Main outcomes ///

eststo clear

eststo panel1: reghdfe n_frnd_nat_lcl_qrtr `nuts3_linear_cntrls' i.nuts3_encoded [aweight=n_qrtr], vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm
eststo panel2: reghdfe n_frnd_nat_lcl_15_16_qrtr `nuts3_linear_cntrls' i.nuts3_encoded [aweight=n_qrtr_15_16], vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm
eststo panel3: reghdfe n_frnd_nat_lcl_17_18_qrtr `nuts3_linear_cntrls' i.nuts3_encoded [aweight=n_qrtr_17_18], vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm
eststo panel4: reghdfe n_frnd_nat_lcl_19_20_qrtr `nuts3_linear_cntrls' i.nuts3_encoded [aweight=n_qrtr_19_20], vce(robust) absorb(i.quarters_since_de `nuts3_FE_cntrls') tol(1e-5)
estadd ysumm

esttab panel* using "output/nuts3_FEs_sy_migrant_quarterly.csv", se label replace

eststo clear